#include "updata_iot.h"//对接fC 云的格式
#include "at_slm332.h"//for 硬件标准。
#include "cjson.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
/**
 *@name  pubsh_AO_information();
 *@brief  处理单个的AO的数据转换成JSON格式
 *@param  up_ao_sum     UINT16格式的源数据（数组） 
 *@param  signal_num    数组长度
 *@param  **out         输出字符串
 *@return 1  完成处理
 *@note   
 */
 
uint8_t Pubsh_AO_information(uint16_t *up_ao_sum, int signal_num, char **out) 
{
    cJSON *root, *fmt;
    char id_str[50];
    root = cJSON_CreateArray();
	  if(up_ao_sum==NULL){
			printf("Erro!,Array * is UNLL.");
		  return -1;
		}
    if(signal_num>16){// Check if length is valid
			printf("Erro!,Array length does not match.");
		  return -1;
		}
        fmt = cJSON_CreateObject();
        sprintf(id_str, "array_00_deviceSgA_signal%02d", signal_num);
        cJSON_AddItemToObject(fmt, "id", cJSON_CreateString(id_str));
        cJSON_AddItemToObject(fmt, "value", cJSON_CreateNumber(*up_ao_sum));
        cJSON_AddItemToObject(fmt, "remark", cJSON_CreateString(""));
        cJSON_AddItemToArray(root, fmt);
    *out = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return 1;
}

/**
 *@name  pubsh_AI_information();
 *@brief  处理单个的AI的数据转换成JSON格式
 *@prame  up_ai_sum     UINT16格式的源数据（数组） 
 *@prame  signal_num    数组长度
 *@prame  **out         输出字符串
 *@return 1 	完成处理
 *@note   
 */
uint8_t Pubsh_AI_information(uint16_t *up_ai_sum, int signal_num, char **out) 
{
    cJSON *root, *fmt;
    char id_str[50];
    root = cJSON_CreateArray();
	  if(up_ai_sum==NULL){
			printf("Erro!,Array * is UNLL.");
		  return -1;
		}
    if(signal_num>16){// Check if length is valid
			printf("Erro!,Array length does not match.");
		  return -1;
		}
        fmt = cJSON_CreateObject();
        sprintf(id_str, "array_00_IdeviceSgA_signalIn%02d", signal_num);
        cJSON_AddItemToObject(fmt, "id", cJSON_CreateString(id_str));
        cJSON_AddItemToObject(fmt, "value", cJSON_CreateNumber(*up_ai_sum));
        cJSON_AddItemToObject(fmt, "remark", cJSON_CreateString(""));
        cJSON_AddItemToArray(root, fmt);

    *out = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return 1;
}
/**
 *@name   pubsh_DO_information();
 *@brief  处理单个的DO的数据转换成JSON格式
 *@prame  up_do_sum     UINT8格式的源数据（数组） 
 *@prame  signal_num    数组长度
 *@prame  **out         输出字符串
 *@return 1  完成处理
 *@note   
 */
uint8_t Pubsh_DO_information(uint8_t *up_do_sum, int signal_num, char **out) 
{
    cJSON *root, *fmt;
    char id_str[50];
    root = cJSON_CreateArray();
	  if(up_do_sum==NULL){
			printf("Erro!,Array * is UNLL.");
		  return -1;
		}
    if(signal_num>10){// Check if length is valid
			printf("Erro!,Array length does not match.");
		  return -1;
		}
 
        fmt = cJSON_CreateObject();
        sprintf(id_str, "array_00_deviceSw_switch%02d", signal_num);
        cJSON_AddItemToObject(fmt, "id", cJSON_CreateString(id_str));
        cJSON_AddItemToObject(fmt, "value", cJSON_CreateNumber(*up_do_sum));
        cJSON_AddItemToObject(fmt, "remark", cJSON_CreateString(""));
        cJSON_AddItemToArray(root, fmt);

    *out = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return 1;
}
/**
 *@name  pubsh_DI_information();
 *@brief  处理单个的DI的数据转换成JSON格式
 *@prame  up_di_sum     UINT8格式的源数据（数组） 
 *@prame  signal_num    数组长度
 *@prame  **out         输出字符串
 *@return 1  完成处理
 *@note   
 */
uint8_t Pubsh_DI_information(uint8_t *up_di_sum, int signal_num, char **out) 
{
    cJSON *root, *fmt;
    char id_str[50];
    root = cJSON_CreateArray();
	  if(up_di_sum==NULL){
			printf("Erro!,Array * is UNLL.");
		  return -1;
		}
    if(signal_num>10){// Check if length is valid
			printf("Erro!,Array length does not match.");
		  return -1;
		}
        fmt = cJSON_CreateObject();
        sprintf(id_str, "array_00_deviceSwIn_switchIn%02d", signal_num);
        cJSON_AddItemToObject(fmt, "id", cJSON_CreateString(id_str));
        cJSON_AddItemToObject(fmt, "value", cJSON_CreateNumber(*up_di_sum));
        cJSON_AddItemToObject(fmt, "remark", cJSON_CreateString(""));
        cJSON_AddItemToArray(root, fmt);
    *out = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return 1;
}


uint8_t pp_js(void *Ptx, uint8_t *dataBuf,uint8_t data_len, uint8_t *cmd_UrcString)
{
    printf("json _ . %s\n", cmd_UrcString);
    return 0; 
}
//上传模拟量输出
void updata_ao(uint16_t *acc,uint8_t length)
{
  char *json_str = NULL;
	for(int i=0;i<length;i++){
        Pubsh_AO_information(&acc[i], i, &json_str);
			  if (json_str != NULL) {
				slm_mqtt_send_mqttdata(SLM, &slm_cfg, slm_mqtt_urc_cb, json_str);
//        printf("Output JSON: %s\n", json_str);
        free(json_str);  
    }
	}
}

//上传模拟量输入
void updata_ai(uint16_t *acc,uint8_t length)
{
  char *json_str = NULL;
	for(int i=0;i<length;i++)
{
        Pubsh_AI_information(&acc[i], i, &json_str);
			  if (json_str != NULL) {
				slm_mqtt_send_mqttdata(SLM, &slm_cfg, pp_js, json_str);
        //printf("Output JSON: %s\n", json_str);
        free(json_str);  // 记得释放 cJSON_PrintUnformatted 分配的内存
    }
	}
}

//上传开关量输出
void updata_do(uint8_t *acc,uint8_t length){
  char *json_str = NULL;
	for(int i=0;i<length;i++)
{
        Pubsh_DO_information(&acc[i], i, &json_str);
			  if (json_str != NULL) {
				slm_mqtt_send_mqttdata(SLM, &slm_cfg, slm_mqtt_urc_cb, json_str);
        printf("Output JSON: %s\n", json_str);
        free(json_str);  // 记得释放 cJSON_PrintUnformatted 分配的内存
    }
	}
}

//上传开关量输出
void updata_di(uint8_t *acc,uint8_t length){
  char *json_str = NULL;
	for(int i=0;i<length;i++)
{
        Pubsh_DI_information(&acc[i], i, &json_str);
			  if (json_str != NULL) {
				slm_mqtt_send_mqttdata(SLM, &slm_cfg, slm_mqtt_urc_cb, json_str);
        printf("Output JSON: %s\n", json_str);
        free(json_str);  // 记得释放 cJSON_PrintUnformatted 分配的内存
    }
	}
}


